package com.zlzkj.app.service;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

//import com.zlzkj.core.sql.SQLBuilder;
import com.zlzkj.core.sql.Row;
//import com.zlzkj.core.mybatis.SqlRunner;
//import com.zlzkj.core.util.Fn;
// import com.zlzkj.app.aop.OperLog;
import com.zlzkj.app.util.IDGenerator;
//import com.zlzkj.app.util.Md5Util;
import com.zlzkj.app.util.Page;
import com.zlzkj.app.util.Excel;
import com.zlzkj.app.mapper.EnergymacMapper;
import com.zlzkj.app.model.Energymac;

@Service
@Transactional
public class EnergymacService {

	@Value("${PAGE_SIZE}")
	private int PAGE_SIZE;

	@Autowired
	private EnergymacMapper mapper;
	
//	@Autowired
//	private SqlRunner sqlRunner;

	// 新增
	// @OperLog(operType="新增用户",userIndex = 0 )
	public Integer save(Energymac entity) {
		entity.setId(IDGenerator.generator());
		return mapper.insert(entity);
	}

	// 根据id删除
	public Integer delete(String id){
		return mapper.deleteByPrimaryKey(id);
	}

	public Integer deletebyIds(String[]id){
		Map parmMap = new HashMap();
		parmMap.put("ids",id);
		return mapper.deleteByMap(parmMap);
	}
	
	//根据主键更新
	public Integer update(Energymac entity){
		return mapper.updateByPrimaryKey(entity);
	}
	
	//根据主键查找
	public Energymac findById(String id){
		return mapper.selectByPrimaryKey(id);
	}

	public List<Row> findByMap(Map<String,Object> map){
		return mapper.selectByMap(map);
	}

	public Page findByMap(Map<String,Object> parmMap,Integer nowPage) {
		if(nowPage == null) nowPage = 1;
		parmMap.put("start",(nowPage-1)*PAGE_SIZE);
		parmMap.put("end",PAGE_SIZE);
		return new Page(findByMap(parmMap),mapper.countByMap(parmMap),nowPage,PAGE_SIZE);
	}
	
	// 导出表
	public ByteArrayOutputStream getExcel() throws IOException{
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();			// 生成流对象
		Excel excel = new Excel();
		excel.doExcel(mapper.selectAll(),mapper.countByMap(null)).write(byteArrayOutputStream);	
		return byteArrayOutputStream;
	}
}
